package com.dyh.quickdemozwei.rxjava;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Test;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextX {

    @Test
    public void test(){
        String k = gentKeys("SF1646784169422","2023121200065325","gift");

        System.out.println(k);
    }

    private static final Pattern TAOBAO_AUTH_NOT_PURCHASE_ERROR_PATTERN = Pattern.compile("^Application (\\d+) need purchase.*");


    @Test public void testXXX(){
        Matcher matcher = TAOBAO_AUTH_NOT_PURCHASE_ERROR_PATTERN.matcher("Application 12345 need purchase x");

        String x = matcher.group(1);

        System.out.println();

    }


    String gentKeys(Object... strArr){
        StringBuilder sb = new StringBuilder();
        for (Object str : strArr) {
            sb.append(str).append("#");
        }
        return sb.substring(0,sb.length()-1);
    }


    @Test public void test123123(){
        Set<String> s = Sets.newHashSet("a","b");

        System.out.println(s.contains(null));
    }


    @Test public void testY(){
        String str = "{\"id\":1,\"type\":0}";

        JSONObject jo = JSON.parseObject(str,JSONObject.class);

        System.out.println();
    }


    @Test public void testXX(){
        String x = "[{\"id\":1,\"type\":0},{\"id\":1,\"type\":1},{\"id\":2,\"type\":1}]\n";

        JSONArray ja = JSON.parseArray(x);


        Map<String, List<JSONObject>> mapl = new HashMap<>();

        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo =  ja.getJSONObject(i);

            List<JSONObject> objectList = mapl.get(jo.getString("id"));
            if (objectList==null){
                objectList = Lists.newArrayList(null,null);
                mapl.put(jo.getString("id"),objectList);
            }

            int idx = jo.getInteger("type");

            objectList.set(idx,jo);

        }


        List<JSONObject> finalList = new ArrayList<>();
        for (List<JSONObject> value : mapl.values()) {
            for (JSONObject jsonObject : value) {
                if (jsonObject!=null){
                    finalList.add(jsonObject);
                    break;
                }
            }
        }

        System.out.println();

    }

    public void arraysParamHandle(JSONObject jo,String... keys){
        for (int i = 0; i < keys.length; i+=2) {
            Object v = jo.get(keys[i]);
            if (v!=null){
                JSONArray ja = jo.getJSONArray(keys[i+1]);
                if (ja==null){
                    ja = new JSONArray();
                    ja.add(v);
                    jo.put(keys[i+1],ja);
                }
                else {
                    ja.add(v);
                }
            }
        }
    }


    @Test public void testxx(){
        JSONObject jo = new JSONObject();
//        jo.put("id",114514L);
        jo.put("name","mur");
        jo.put("names",Lists.newArrayList("yjsnp"));


        arraysParamHandle(jo,
                "id","ids",
                "name","names");


        System.out.println(JSON.toJSONString(jo));

    }
}
